//
//  TMDWBTabBar.swift
//  TMDWeiBo
//
//  Created by 汤明鼎 on 2017/1/17.
//  Copyright © 2017年 heimaitcast. All rights reserved.
//

import UIKit

class TMDWBTabBar: UITabBar {

    var closure:(()->())?
    
    override init(frame:CGRect){
    
        super.init(frame: frame)
        setupUI()
        
    }
    //懒加载按钮
    private lazy var composeButton:UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        btn.sizeToFit()
        return btn
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(composeButton)
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = TMDScreenW * 0.2
        var index:CGFloat = 0
        for view in subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.frame.size.width = width
                view.frame.origin.x = index * width
                
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }
        
        composeButton.center.x = self.center.x
        composeButton.center.y = self.bounds.size.height*0.5

    }
    
    @objc private func btnClick(){
        
        closure?()
    }
}
